﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication.AppCode;
using BLL;
using Newtonsoft.Json;
using Model;

namespace WebApplication.Admin
{
    public partial class MenuForm : BasePage
    {
        SysBLL bll = new SysBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request["mode"])
            {
                case "add":
                    ID.Value = Guid.Empty.ToString();
                    break;
                case "edit":
                    Guid id;
                    if (!Guid.TryParse(Request["id"], out id))
                    {
                        Alert("编号错误！"); return;
                    }
                    var menu = bll.DbContext.Menus.FirstOrDefault(x => x.ID == id);
                    if (menu == null)
                    {
                        Alert("数据不存在！"); return;
                    }
                    RegisterScript("SetData(" + JsonConvert.SerializeObject(menu) + ")");
                    if (menu.PID.HasValue)
                    {
                        var parent = bll.DbContext.Menus.FirstOrDefault(x => x.ID == menu.PID);
                        if (parent != null)
                        {
                            RegisterScript("mini.get('PID').setText('" + parent.Text + "');");
                        }
                    }
                    break;
            }
        }
    }
}